package com.parking.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.model.StateData;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.aop.Permission;
import com.parking.common.web.util.RedisUtil;
import com.parking.platform.dto.PlatformAccountDto;
import com.parking.platform.dto.PlatformDto;
import com.parking.platform.entity.PlatformAccount;
import com.parking.platform.exception.PlatformException;
import com.parking.platform.exception.PlatformExceptionCode;
import com.parking.platform.form.AddPlatformDataForm;
import com.parking.platform.form.PlatLoginForm;
import com.parking.platform.form.SelPlatformForm;
import com.parking.platform.form.UpPlatformPassForm;
import com.parking.platform.service.PlatformAccountService;
import com.parking.platform.service.PlatformRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/platformAccount")
@Api(tags = "平台登录接口")
public class PlatformAccountController {
    @Resource
    private PlatformAccountService platformAccountService;
    @Resource
    private PlatformRoleService platformRoleService;
    @Resource
    private RedisUtil redisUtil;


    /***
     * 登录方法
     * 参数：
     * ("平台账号")：platformAccount;
     * ("平台密码")：platformPass;
     * ("请求头"):response
     */
    @PostMapping("/platLogin")
    @ApiOperation("平台登录")
    @Transactional
    public Result platLogin(@RequestBody PlatLoginForm form, HttpServletResponse response, HttpServletRequest request){
        String IpPath = request.getRemoteAddr();
        PlatformDto dto=platformAccountService.platLogin(form,IpPath);
        //将平台管理员信息存进Token
        String refreshToken = JwtUtil.createAdminToken(3 * 24 * 60,"platformId",dto.getPlatformId());
        String accessToken = JwtUtil.createAdminToken(300,"platformId",dto.getPlatformId());
        System.out.println(accessToken+"短token参数");
        System.out.println(refreshToken+"长token参数");
        response.setHeader("authorization", accessToken);
        redisUtil.set(accessToken,refreshToken,3*24*60*60L);
        response.setHeader("Access-Control-Expose-Headers", "authorization");
        return Result.getResult(PlatformDto.class).setMessage("登录成功").setCode(200).setData(dto);
    }
    /***
     * 最高管理员
     * 添加平台账号
     * 参数：
     * 令牌：authorization=>adminId
     * ("平台账号")：platformAccount;
     * ("平台密码")：platformPass;
     */
    @ApiOperation("添加平台账号")
    @PostMapping("/addPlatformData.platform")
    @Transactional
    @Permission(resources = {"/addPlatformData.platform"})
    public Result addPlatformData(@RequestBody @Valid AddPlatformDataForm form,@RequestHeader("authorization")String authorization)throws Exception{
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.addPlatformData(form,adminId);
        return Result.getResult().setCode(200).setMessage("添加平台账号成功");
    }
    /***
     * 最高管理员
     * 修改平台账号
     * 参数：
     * 令牌：authorization=>adminId
     * 令牌：authorization
     */
    @ApiOperation("修改平台账号")
    @Transactional
    @GetMapping("/upPlatformData.platform")
    @Permission(resources = {"/upPlatformData.platform"})
    public Result upPlatformData(@RequestHeader("authorization")String authorization,String platformAccount,Integer platformId) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.upPlatformData(platformId,platformAccount,adminId);
        return Result.getResult().setCode(200).setMessage("修改平台账号成功");
    }
    /***
     * 根据Id查询平台账号
     * 参数：
     * 令牌:authorization
     */
    @ApiOperation("根据Id查询平台账号")
    @GetMapping ("/findPlatformById.platform")
    @Permission(resources = {"/findPlatformById.platform"})
    public Result findPlatformById(@RequestHeader("authorization")String authorization) throws Exception {
        System.out.println("进入");
        String refreshToken = redisUtil.get(authorization);
        System.out.println("参数tokem:"+refreshToken);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        PlatformDto dto=platformAccountService.findPlatformById(platformId);
        return Result.getResult(PlatformDto.class).setCode(200).setMessage("根据Id查询账号成功").setData(dto);
    }
    /**
     * 最高管理员
     * 启用平台账号状态
     * 参数
     * 令牌：authorization=>adminId
     * 账号状态：state
     * 账号Id：platformId
     * */
    @GetMapping("/enableStateById.platform")
    @ApiOperation("/启用平台账号状态接口")
    @Permission(resources = {"/enableStateById.platform"})
    public Result enablePlatStateById(@RequestHeader("authorization")String authorization,Integer platformId) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.enablePlatStateById(platformId,adminId);
        return Result.getResult().setCode(200).setMessage("启用平台账号状态成功");
    }
    /**
     * 最高管理员
     * 禁用平台账号状态
     * 参数
     * 令牌：authorization=>adminId
     * 账号状态：state
     * 账号Id：platformId
     * */
    @GetMapping("/downStateById.platform")
    @ApiOperation("/禁用平台账号状态接口")
    @Permission(resources = {"/downStateById.platform"})
    public Result  downPlatStateById(@RequestHeader("authorization")String authorization,Integer platformId) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.downPlatStateById(platformId,adminId);
        return Result.getResult().setCode(200).setMessage("禁用平台账号状态成功");
    }
    /**
     * 最高管理员
     * 删除平台账号
     * 参数
     * 令牌：authorization=>adminId
     * 账号状态：state
     * 账号Id：platformId
     * */
    @GetMapping("/deleteStateById.platform")
    @ApiOperation("/删除平台账号接口")
    @Permission(resources = {"/deleteStateById.platform"})
    public Result  deletePlatStateById(@RequestHeader("authorization")String authorization,Integer platformId) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.deletePlatStateById(platformId,adminId);
        return Result.getResult().setCode(200).setMessage("删除平台账号成功");
    }
    /***
     * 最高管理员
     * 查询全部平台账号
     * 参数：
     * 账号：platformAccount
     * 角色：platformRoleName
     * 返回值：
     *  List<PlatformDto>
     *
     */
    @PostMapping("/selPlatformForm.platform")
    @ApiOperation("查询全部平台账号")
    @Permission(resources = {"/selPlatformForm.platform"})
    public Result findPlatformAll(@RequestBody SelPlatformForm form){
        PageInfo page=platformAccountService.findPlatformAll(form);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("查询全部平台账号").setData(page);
    }

    /***
     * 退出登录方法
     * 参数：
     ("平台Id")：platformId;
     */
    @GetMapping("/platformLogOff.platform")
    @Permission(resources = {"/platformLogOff.platform"})
    @ApiOperation("用户退出登录")
    public Result PlatformLogOff(@RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.PlatformLogOff(platformId);
        return Result.getResult().setMessage("退出登录成功").setCode(200);
    }

    /**
     * 平台修改密码
     * 参数：
     *  ("旧密码")：oldPass;
     * ("新密码")：newPass;
     *  ("平台Id")：platformId;
     * */
    @PostMapping("/UpPlatformPass.platform")
    @Permission(resources = {"/UpPlatformPass.platform"})
    @ApiOperation("平台修改密码")
    public Result UpPlatformPass(@RequestBody UpPlatformPassForm form, @RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        platformAccountService.UpPlatformPass(form,platformId);
        return Result.getResult().setCode(200).setMessage("平台修改密码成功");
    }


















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**  模块内调用方法
     * */

    /**
     * 根据ID查询平台账号
     * 参数: 平台账号Id platformId
     * 返回: 平台账号Id platformId
     * 平台账号 platformAccount
     * 方法: 1.判断platformId是否存在对应的平台账号 若不存在则抛异常
     * 2.判断对应的平台账号的状态是否是启用的 若不是则抛异常
     * 3.将平台账号放入Dto返回
     */
    @GetMapping("/selectPlatformAccountById")
    @ApiOperation("根据ID查询平台账号")
    public  Result selectPlatformAccountById(@RequestParam("platformId") Integer platformId){
        PlatformAccount account = platformAccountService.getPlatformById(platformId);
        //账号是否存在
        if (account==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_NO);
        }
        //账号状态是否正常
        if (account.getPlatformState()!= StateData.PLATFORM_STATE_ENABLE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_STATE_NO);
        }
        PlatformAccountDto dto = BeanUtil.toBean(account, PlatformAccountDto.class);
        return Result.getResult(PlatformAccountDto.class).setCode(200).setMessage("根据ID查询平台账号成功").setData(dto);
    }


}

